/*
 * @(#) CreatMeetingControl
 *
 * <br> Copyright:  Copyright (c) 2017
 * <br> @author caijinhuang
 * <br> 2017-12-18 16:01:11
 */

package com.sunsharing.sharemeeting.web.controll.meeting;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunsharing.sharemeeting.service.api.MeetingService;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.sunsharing.sharemeeting.constant.FileUrl;
import com.sunsharing.sharemeeting.constant.DataState;
import com.sunsharing.sharemeeting.entity.ResponseModel;
import com.sunsharing.sharemeeting.entity.User;

@Controller
@RequestMapping("/meeting")
public class CreatMeetingControl {
    @Resource
    DataState dataState;
    @Resource
    MeetingService meetingService;

    /**
     * 基本信息：会议记录基本信息获取
     * @return
     */
    @RequestMapping(value = "/getBaseInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public String getBaseInfo() {
        Map<String, Object> baseMap = dataState.getAll();
        ResponseModel responseModel = new ResponseModel(true, "获取成功", baseMap);
        return JSON.toJSONString(responseModel);
    }

    /**
     * 保存记录：保存会议记录
     * @param params
     *  参数：
     *      params: {
     *           meetingId:'',//会议id
     *           meetingTitle: '',//会议主题
     *           meetingPlace: '',//会议地点
     *           meetingPresenter: '',//主持人
     *           meetingContent: '',//会议内容
     *           //会议决议
     *           meetingDecision: [{
     *              decisionContent:"",//决议内容
     *              members:[],//决议执行人
     *              decisionEndTime:"",//截至日期
     *              priority:"",//决议优先级
     *           }],
     *              attenderMember: [{userId:'',state:''}],//所有参会人员
     *           },
     * @return
     */
    @RequestMapping(value = "/savaMeeting.do", method = RequestMethod.POST)
    @ResponseBody
    public String saveMeeting(@RequestBody JSONObject params, HttpSession session) {
        ResponseModel responseModel = null;
        User user = (User) session.getAttribute("user");
        String id = meetingService.saveMeeting(params, user.getUserId());
        if (id != null) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("id", id);
            responseModel = new ResponseModel(true, "保存成功", map);
        } else {
            responseModel = new ResponseModel(false, "操作失败", null);
        }
        return JSON.toJSONString(responseModel);
    }

    /**
     * 文件：多文件上传
     * @param meetingId 会议Id
     * @param files 会议附件
     * @return
     */
    @RequestMapping(value = "/upload.do", method = RequestMethod.POST)
    @ResponseBody
    public String addPath(@RequestParam("meetingId") String meetingId, @RequestParam("file") MultipartFile[] files, HttpSession session)
        throws IOException {
        ResponseModel responseModel = null;
        List<String> fileNames = new ArrayList<String>();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getOriginalFilename();
                //文件后缀名
                String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
                //文件名
                fileName = fileName.substring(0, fileName.lastIndexOf(".")) + Calendar.getInstance().getTimeInMillis() + fileSuffix;
                fileNames.add(fileName);
                File file = new File(FileUrl.FILELOCAL, fileName);
                if (!file.exists()) {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                FileUtils.copyInputStreamToFile(files[i].getInputStream(), file);
            }
            Map<String,String> resultList = meetingService.uploadFile(fileNames, meetingId,
                ((User) session.getAttribute("user")).getUserId());
            if (! resultList.isEmpty()) {
                responseModel = new ResponseModel(true, "上传成功", resultList);
            } else {
                responseModel = new ResponseModel(false, "上传失败，请重新尝试", null);
            }
        }
        return JSON.toJSONString(responseModel);
    }

    /**
     * 删除附件
     * @param params = {
     *         fileName:附件名称
     *         fileId:附件id
     * }
     * @return
     */
    @RequestMapping(value = "/deletfile.do", method = RequestMethod.POST)
    @ResponseBody
    public String deletFile(@RequestBody JSONObject params) {
        ResponseModel responseModel = null;
        if (meetingService.delFile(params.getString("fileName"),params.getString("fileId")) > 0) {
            responseModel = new ResponseModel(true,"文件已删除",null);
        } else {
            responseModel = new ResponseModel(false,"删除失败",null);
        }
        return JSON.toJSONString(responseModel);
    }
}
